﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using DevComponents.DotNetBar.Metro;
using ProductManagement.Ultilities;

namespace ProductManagement.Presentation_Layer
{
    public partial class ImportForm : MetroForm
    {
        #region Variables

        private int _rid;
        private ProductManagementClassesDataContext db = new ProductManagementClassesDataContext();
        private Validator validator = new Validator();
        private MainForm _mainForm;

        #endregion

        #region Constructors

        public ImportForm(int reportId, MainForm mainForm)
        {
            _rid = reportId;
            _mainForm = mainForm;
            InitializeComponent();
        }

        #endregion

        #region Events Handling

        private bool ValidateImportQuantity()
        {
            if (validator.ValidateQuantity(txtImportQuantity, errorProvider1))
            {
                if (Int32.Parse(txtImportQuantity.Text) == 0)
                {
                    errorProvider1.SetError(txtImportQuantity, "Số lượng sản phẩm phải lớn hơn 0!");
                    return false;
                }
                else
                    return true;
            }
            else
                return false;
        }

        private bool ValidateImportData()
        {
            bool isValidName = validator.ValidateName(txtImportName, errorProvider1);
            bool isValidPrice = validator.ValidatePrice(txtImportPrice, errorProvider1);
            bool isValidQuantity = ValidateImportQuantity();
            bool isValidYear = validator.ValidateYear(txtImportYear, errorProvider1);
            return (isValidName && isValidPrice && isValidQuantity && isValidYear);
        }

        private void ResetTextFields()
        {
            txtImportName.ResetText();
            txtImportPrice.ResetText();
            txtImportQuantity.ResetText();
            txtImportYear.ResetText();
            errorProvider1.Dispose();
        }

        private void btnAddImport_Click(object sender, EventArgs e)
        {
            if (ValidateImportData())
            {
                string name = txtImportName.Text;
                decimal price = Decimal.Parse(txtImportPrice.Text);
                int quantity = Int32.Parse(txtImportQuantity.Text);
                int year = Int32.Parse(txtImportYear.Text);
                string state = (quantity > 0) ? "Còn hàng" : "Hết hàng";
                var count = db.sp_IsExistProduct(name, price, year).SingleOrDefault();
                if (count.dem != 0)
                {
                    int productID = db.sp_GetProductID(name, price, year).SingleOrDefault().id;
                    int oldQuantity = db.sp_GetProductQuantity(productID).SingleOrDefault().quantity;
                    db.sp_UpdateProductQuantity(oldQuantity + quantity, productID);
                }
                else
                {
                    db.sp_InsertProduct(name, price, quantity, year, state);
                }
                db.sp_InsertReportDetail(_rid, name, price, quantity);
                db.SubmitChanges();
                MainForm.DisplayExportImportInfo(_rid, _mainForm);
                MessageBoxEx.Show("Nhập sản phẩm thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void btnResetImport_Click(object sender, EventArgs e)
        {
            ResetTextFields();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #endregion
    }
}
